#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/1/04 15:50
# @Author  : Beam

def __virtual__():
    '''The name of the call'''
    return "returnpost"

def returner(ret):
    import hashlib, time
    import requests
    import json
    key = 'ucuxeyaBusiep6chei5aicohnaeNgei2'  # server端定义提供的key
    key_name = 'auth-key'  # 该值为固定的参数，匹配server端的key
    asset_api = 'http://192.168.0.10:8000/api/getminions/'  # 接口url
    ha = hashlib.md5(key.encode('utf-8'))  # 用key 做加密验证
    time_span = time.time()
    ha.update(bytes("%s|%f" % (key, time_span)))   #python2用这句
    #ha.update(bytes("%s|%f" % (key, time_span), encoding='utf-8'))   #python3用这句
    encryption=ha.hexdigest()
    result = "%s|%f" % (encryption, time_span)
    headers = {}
    headers.update({key_name: result})  # 把auth_key()这个方法里返回的字典加入到headers
    #ret['return'] = str(ret['return'])
    data = json.dumps(dict(ret),ensure_ascii=False)
    response = requests.get(url=asset_api,headers=headers,data=data)
    request = json.loads(response.text)
    if request['isFalse']:
        info = str('Success ')+str(ret['schedule'])+' '+str(time.time())
        f = open('/tmp/minions_post.log', 'a+')
        f.write(str(info) + '\n')
        f.close()
    else:
        error = str('Error ')+str(ret['schedule'])+' '+str(time.time())
        f = open('/tmp/minions_post.log', 'a+')
        f.write(str(error) + '\n')
        f.close()

